<?

function makeDecision($id, $boardMember, $decision, $comment) {
	$sql = "UPDATE kppstatusreviewitem set decision = '$decision' where kppstatusreviewid = $id and boardMember = '$boardMember';";
	mysql_query($sql);
	
	if ($comment != null && $comment != "") {
		$sql = "INSERT INTO kppstatusreviewcomment (kppstatusreviewid, boardmember, comment) values ($id, '$boardMember', '" . str_replace("'", "''", $comment) . "');";
		mysql_query($sql);
	}
	
}

function addComment($id, $boardMember, $comment) {
	if ($comment != null && $comment != "") {
 		$sql = "INSERT INTO kppstatusreviewcomment (kppstatusreviewid, boardmember, comment) values ($id, '$boardMember', '" . str_replace("'", "''", ereg_replace("/\n\r|\r\n|/n/", "<br>", $comment)) . "');";
	   	mysql_query($sql);
	}
}

function addReview($review, $playerId) {
	$sql = "INSERT INTO kppstatusreview (playerId, description) values ($playerId, '$review');";
	mysql_query($sql);
	
	$id = mysql_insert_id();
	
	$array = KPPStatusBoard::getAll();
	$count = count($array);
	for ($i = 0; $i < $count; $i++) {
		$sql = "INSERT INTO kppstatusreviewitem (kppStatusReviewId, boardMember) values ($id, '$array[$i]');";
		mysql_query($sql);
	}
	
}

function getKPPStatusReviews($page, $maxPerPage) {
	$sql = "select kpp.id as kppId, description, raiserComment, firstName, surname, team, playerType from kppstatusreview kpp inner join player p on kpp.playerId = p.id order by kpp.id desc";
	
	$result = mysql_query($sql);
	
	$array = array();
        $count = 1;
	while($row = mysql_fetch_array($result)) {
           if (($count <= $page*$maxPerPage && ($count > $page*$maxPerPage - $maxPerPage)) || $page == -1) {
		$review = new KPPStatusReview();
		$review->id = $row['kppId'];
		$review->description = $row['description'];
		$review->raiserComment = $row['raiserComment'];
		$player = new Player();
		$player->playerFirstName = $row['firstName'];
		$player->playerSurname = $row['surname'];
		$player->team = $row['team'];
		$player->playerType = $row['playerType'];
		$review->player = $player;
		$review->boardMemberDecisions = getKPPStatusReviewItems($review->id);
		$review->comments = getKPPStatusReviewComments($review->id);
		
		$array[] = $review;
           }
           $count++;
	}
	
	
	return $array;
}

function getNumberKPPStatusReviews() {
	$sql = "select count(*) as count from kppstatusreview kpp inner join player p on kpp.playerId = p.id";
	
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$result = $row['count'];
	
	
	return $result;
}

function getNumberUndecidedKPPStatusReviews($boardMember) {
	$sql = "select count(*) as count from kppstatusreviewitem where boardMember = '$boardMember' and decision is NULL";
	
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$result = $row['count'];
	
	
	return $result;
}

function getKPPStatusReviewItems($id) {
	$sql = "select * from kppstatusreviewitem where kppstatusreviewid = $id";
	
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$reviewItem = new KPPStatusReviewItem();
		$reviewItem->boardMember = $row['boardMember'];
		$reviewItem->decision = $row['decision'];
		
		$array[] = $reviewItem;
	}
	
	return $array;
}

function getKPPStatusReviewComments($id) {
	$sql = "select * from kppstatusreviewcomment where kppstatusreviewid = $id";
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$reviewItem = new KPPStatusReviewComment();
		$reviewItem->boardMember = $row['boardMember'];
		$reviewItem->comment = $row['comment'];
		
		$array[] = $reviewItem;
	}
	
	return $array;
}

?>